<template>
  <div class="instruction-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的授课</span>
        </div>
      </template>
      <el-table :data="instructionList" border style="width: 100%">
        <el-table-column prop="course_name" label="课程名称" />
        <el-table-column prop="course_credit" label="学分" />
        <el-table-column prop="course_time" label="上课时间" :formatter="formatterTime" />
        <el-table-column prop="course_location" label="上课地点" />
        <el-table-column prop="course_selected_count" label="已选人数" />
        <el-table-column prop="course_max_size" label="最大人数" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button size="small" type="primary" @click="goSource(row)">成绩录入</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getInstructionListAPI } from '@/api/teacher/instruction.js'
import { weeks } from '@/constant'

const instructionList = ref([])
const router = useRouter()

const fetchInstructionList = async () => {
  const data = await getInstructionListAPI()
  // 拦截器已返回 data.data，这里直接赋值
  instructionList.value = data
}

const goSource = (row) => {
  // 跳转到教师成绩录入页，并带上课程名作为默认筛选
  router.push({
    path: '/home/teacher/source',
    query: { cname: row.course_name }
  })
}

const formatterTime = (row, column, cellValue) => {
  if (!cellValue) return ''
  const [w, s, e] = cellValue.split('-')
  return `${weeks[w]} 第${parseInt(s) + 1}至${parseInt(s) + parseInt(e)}节`
}

onMounted(fetchInstructionList)
</script>

<style lang="scss" scoped>
.instruction-container {
  padding: 20px;
}
</style>
